home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Messaging Service Access Module / Internet PMSAM / Internet PMSAM source / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  6.9 KB  |  307 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5.     Modifications by Jim Matthews, Dartmouth College, 5/91
  6.     
  7.     Further modifications by Steve Falkenburg, Apple MacDTS 8/91
  8.     
  9. */
  10.  
  11. #ifndef __OSUTILS__
  12. #include <OSUtils.h>
  13. #endif
  14.  
  15. #ifndef __ERRORS__
  16. #include <Errors.h>
  17. #endif
  18.  
  19. #ifndef __FILES__
  20. #include <Files.h>
  21. #endif
  22.  
  23. #ifndef __RESOURCES__
  24. #include <Resources.h>
  25. #endif
  26.  
  27. #ifndef __MEMORY__
  28. #include <Memory.h>
  29. #endif
  30.  
  31. #ifndef __TRAPS__
  32. #include <Traps.h>
  33. #endif
  34.  
  35. #ifndef __GESTALTEQU__
  36. #include <GestaltEqu.h>
  37. #endif
  38.  
  39. #ifndef __FOLDERS__
  40. #include <Folders.h>
  41. #endif
  42.  
  43. #ifndef __TOOLUTILS__
  44. #include <ToolUtils.h>
  45. #endif
  46.  
  47. #include <MacTCPCommonTypes.h>
  48. #include <AddressXLation.h>
  49.  
  50. // think C compatibility stuff
  51.  
  52. #ifndef    _GestaltDispatch
  53. #define    _GestaltDispatch    _Gestalt
  54. #endif
  55.  
  56. #include "trapavailable.h"
  57.  
  58. #define OPENRESOLVER    1L                    /* need to make these longs explicit, even though... */
  59. #define CLOSERESOLVER    2L                    /* ...the prototype below would do it normally.  This*/
  60. #define STRTOADDR        3L                    /* ...is a workaround for a bug in Think C 4.0.5.     */
  61. #define    ADDRTOSTR        4L
  62. #define    ENUMCACHE        5L
  63. #define ADDRTONAME        6L
  64. #define    HINFO            7L
  65. #define MXINFO            8L
  66.  
  67. void GetSystemFolder(short *vRefNumP, long *dirIDP);
  68. void GetCPanelFolder(short *vRefNumP, long *dirIDP);
  69. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID);
  70. short OpenOurRF(void);
  71.  
  72. Handle codeHndl = nil;
  73.  
  74. //typedef OSErr (*OSErrProcPtr)(long,...);    /* added proto for Think C compatibility -->SJF<-- */
  75. OSErrProcPtr dnr = nil;                        /* ... otherwise, first arg would be passed as short*/
  76.  
  77.  
  78. void GetSystemFolder(short *vRefNumP, long *dirIDP)
  79. {
  80.     SysEnvRec info;
  81.     long wdProcID;
  82.     
  83.     SysEnvirons(1, &info);
  84.     if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
  85.         *vRefNumP = 0;
  86.         *dirIDP = 0;
  87.         }
  88.     }
  89.  
  90. void GetCPanelFolder(short *vRefNumP, long *dirIDP)
  91. {
  92.     Boolean hasFolderMgr = false;
  93.     long feature;
  94.     
  95.     if (TrapAvailable(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  96.     if (!hasFolderMgr) {
  97.         GetSystemFolder(vRefNumP, dirIDP);
  98.         return;
  99.         }
  100.     else {
  101.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
  102.             *vRefNumP = 0;
  103.             *dirIDP = 0;
  104.             }
  105.         }
  106.     }
  107.     
  108. /* SearchFolderForDNRP is called to search a folder for files that might 
  109.     contain the 'dnrp' resource */
  110. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
  111. {
  112.     HParamBlockRec fi;
  113.     Str255 filename;
  114.     short refnum;
  115.     
  116.     fi.fileParam.ioCompletion = nil;
  117.     fi.fileParam.ioNamePtr = filename;
  118.     fi.fileParam.ioVRefNum = vRefNum;
  119.     fi.fileParam.ioDirID = dirID;
  120.     fi.fileParam.ioFDirIndex = 1;
  121.     
  122.     while (PBHGetFInfo(&fi, false) == noErr) {
  123.         /* scan system folder for driver resource files of specific type & creator */
  124.         if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
  125.             fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
  126.             /* found the MacTCP driver file? */
  127.             refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
  128.             if (GetIndResource('dnrp', 1) == NULL)
  129.                 CloseResFile(refnum);
  130.             else
  131.                 return refnum;
  132.             }
  133.         /* check next file in system folder */
  134.         fi.fileParam.ioFDirIndex++;
  135.         fi.fileParam.ioDirID = dirID;    /* PBHGetFInfo() clobbers ioDirID */
  136.         }
  137.     return(-1);
  138.     }    
  139.  
  140. /* OpenOurRF is called to open the MacTCP driver resources */
  141.  
  142. short OpenOurRF(void)
  143. {
  144.     short refnum;
  145.     short vRefNum;
  146.     long dirID;
  147.     
  148.     /* first search Control Panels for MacTCP 1.1 */
  149.     GetCPanelFolder(&vRefNum, &dirID);
  150.     refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
  151.     if (refnum != -1) return(refnum);
  152.         
  153.     /* next search System Folder for MacTCP 1.0.x */
  154.     GetSystemFolder(&vRefNum, &dirID);
  155.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  156.     if (refnum != -1) return(refnum);
  157.         
  158.     /* finally, search Control Panels for MacTCP 1.0.x */
  159.     GetCPanelFolder(&vRefNum, &dirID);
  160.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  161.     if (refnum != -1) return(refnum);
  162.         
  163.     return -1;
  164.     }    
  165.  
  166.  
  167. OSErr OpenResolver(fileName)
  168. char *fileName;
  169. {
  170.     short refnum;
  171.     OSErr rc;
  172.     
  173.     if (dnr != nil)
  174.         /* resolver already loaded in */
  175.         return(noErr);
  176.         
  177.     /* open the MacTCP driver to get DNR resources. Search for it based on
  178.        creator & type rather than simply file name */    
  179.     refnum = OpenOurRF();
  180.  
  181.     /* ignore failures since the resource may have been installed in the 
  182.        System file if running on a Mac 512Ke */
  183.        
  184.     /* load in the DNR resource package */
  185.     codeHndl = GetIndResource('dnrp', 1);
  186.     if (codeHndl == nil) {
  187.         /* can't open DNR */
  188.         return(ResError());
  189.         }
  190.     
  191.     DetachResource(codeHndl);
  192.     if (refnum != -1) {
  193.         CloseResFile(refnum);
  194.         }
  195.         
  196.     /* lock the DNR resource since it cannot be reloated while opened */
  197.     MoveHHi(codeHndl);
  198.     HLock(codeHndl);
  199.     dnr = (OSErrProcPtr) *codeHndl;
  200.     
  201.     /* call open resolver */
  202.     rc = (*dnr)(OPENRESOLVER, fileName);
  203.     if (rc != noErr) {
  204.         /* problem with open resolver, flush it */
  205.         HUnlock(codeHndl);
  206.         DisposHandle(codeHndl);
  207.         dnr = nil;
  208.         }
  209.     return(rc);
  210.     }
  211.  
  212.  
  213. OSErr CloseResolver()
  214. {
  215.     if (dnr == nil)
  216.         /* resolver not loaded error */
  217.         return(notOpenErr);
  218.         
  219.     /* call close resolver */
  220.     (void) (*dnr)(CLOSERESOLVER);
  221.  
  222.     /* release the DNR resource package */
  223.     HUnlock(codeHndl);
  224.     DisposHandle(codeHndl);
  225.     dnr = nil;
  226.     return(noErr);
  227.     }
  228.  
  229. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  230. char *hostName;
  231. struct hostInfo *rtnStruct;
  232. ResultProcPtr resultproc;
  233. char *userDataPtr;
  234. {
  235.     if (dnr == nil)
  236.         /* resolver not loaded error */
  237.         return(notOpenErr);
  238.         
  239.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  240.     }
  241.     
  242. OSErr AddrToStr(addr, addrStr)
  243. unsigned long addr;
  244. char *addrStr;                                    
  245. {
  246.     if (dnr == nil)
  247.         /* resolver not loaded error */
  248.         return(notOpenErr);
  249.         
  250.     (*dnr)(ADDRTOSTR, addr, addrStr);
  251.     return(noErr);
  252.     }
  253.     
  254. OSErr EnumCache(resultproc, userDataPtr)
  255. EnumResultProcPtr resultproc;
  256. char *userDataPtr;
  257. {
  258.     if (dnr == nil)
  259.         /* resolver not loaded error */
  260.         return(notOpenErr);
  261.         
  262.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  263.     }
  264.     
  265.     
  266. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  267. unsigned long addr;
  268. struct hostInfo *rtnStruct;
  269. ResultProcPtr resultproc;
  270. char *userDataPtr;                                    
  271. {
  272.     if (dnr == nil)
  273.         /* resolver not loaded error */
  274.         return(notOpenErr);
  275.         
  276.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  277.     }
  278.  
  279.  
  280. extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  281. char *hostName;
  282. struct returnRec *returnRecPtr;
  283. ResultProc2Ptr resultProc;
  284. char *userDataPtr;
  285. {
  286.     if (dnr == nil)
  287.         /* resolver not loaded error */
  288.         return(notOpenErr);
  289.         
  290.     return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  291.  
  292.     }
  293.     
  294. extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  295. char *hostName;
  296. struct returnRec *returnRecPtr;
  297. ResultProc2Ptr resultProc;
  298. char *userDataPtr;
  299. {
  300.     if (dnr == nil)
  301.         /* resolver not loaded error */
  302.         return(notOpenErr);
  303.         
  304.     return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  305.  
  306.     }    /* removed ; (causes syntax err in Think C 5.0 */
  307.